<?php
/**
 * Created by PhpStorm.
 * User: xiaomage
 * Date: 2020/12/8
 * Time: 10:06
 */

namespace app\system\controller;

use common\ApiReturn;
use common\security\JsonToken;
use component\system\system_user\SystemUser;
use think\facade\Event;

class User
{
    /**
     * @return \think\Response
     */
    public function login()
    {
        $username = request()->post('username');
        $password = request()->post('password');

        $record = SystemUser::instance()->login($username, $password);

        $result = false;

        if ($record) {
            $result = [];
            $result['authorize'] = JsonToken::instance()->makeToken($record->id);
            $result['info'] = $record;
            // 监听用户登录
            Event::trigger('UserLogin', $record);
            $result['other'] = [
                'ip' => request()->ip(),
                'time' => date('Y-m-d H:i'),
            ];
        }

        return ApiReturn::auto($result);
    }

    /**
     * 退出登录
     * @return \think\Response
     */
    public function logout()
    {
        return ApiReturn::auto(true);
    }
}